VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsRunProcess"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Declare Function CreateProcess Lib "kernel32.dll" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByRef lpProcessAttributes As Any, ByRef lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByRef lpEnvironment As Any, ByVal lpCurrentDriectory As String, ByRef lpStartupInfo As STARTUPINFO, ByRef lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByRef lpExitCode As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32.dll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Private Type STARTUPINFO
    cb As Long
    lpReserved As Long
    lpDesktop As Long
    lpTitle As Long
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Byte
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Const WAIT_TIMEOUT As Long = 258&
Private Const STATUS_PENDING As Long = &H103
Private Const STILL_ACTIVE As Long = STATUS_PENDING

Private m_pi As PROCESS_INFORMATION

Friend Property Get Handle() As Long
Handle = m_pi.hProcess
End Property

Friend Property Get ProcessId() As Long
ProcessId = m_pi.dwProcessId
End Property

Friend Function Run(ByVal lpCommandLine As String, ByVal lpCurrentDriectory As String) As Boolean
Dim si As STARTUPINFO
If m_pi.hProcess Then Exit Function
si.cb = Len(si)
If CreateProcess(vbNullString, lpCommandLine, ByVal 0, ByVal 0, 0, 0, ByVal 0, lpCurrentDriectory, si, m_pi) = 0 Then Exit Function
CloseHandle m_pi.hThread 'we don't need it
m_pi.hThread = 0
m_pi.dwThreadId = 0
Run = True
End Function

Friend Sub Detach()
If m_pi.hProcess = 0 Then Exit Sub
CloseHandle m_pi.hProcess
m_pi.hProcess = 0
End Sub

Friend Function Wait(ByVal dwMilliseconds As Long) As Boolean
If m_pi.hProcess = 0 Then Exit Function
Wait = WaitForSingleObject(m_pi.hProcess, dwMilliseconds) = WAIT_TIMEOUT
End Function

Friend Function GetExitCode() As Long
Dim i As Long
If m_pi.hProcess = 0 Then Exit Function
If GetExitCodeProcess(m_pi.hProcess, i) Then GetExitCode = i
End Function

Friend Function Terminate() As Boolean
If m_pi.hProcess = 0 Then Exit Function
Terminate = TerminateProcess(m_pi.hProcess, -1) <> 0
CloseHandle m_pi.hProcess
m_pi.hProcess = 0
End Function

Private Sub Class_Terminate()
Terminate
End Sub
